package studentdemo;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-08-01
 * Time: 19:48
 */
public class TestDemo {



    public static void reflectPrivateConstructor() {
        try {
            //1.
            Class<?> c1 = Class.forName("studentdemo.Student");

            Constructor<Student> constructor =
                    (Constructor<Student>) c1.getDeclaredConstructor(String.class, int.class);
            //私有的一般都要加
            constructor.setAccessible(true);

           Student student = constructor.newInstance("zhangs", 13);

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

        public static void reflectInstance() {

        try {
            //1.
            Class<?> c1 = Class.forName("studentdemo.Student");
           Student student = (Student) c1.newInstance();
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateField() {
        try {
            //1.
            Class<?> c1 = Class.forName("studentdemo.Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);

            Student student = (Student) c1.newInstance();
            field.set(student,"wangda");
            System.out.println(student);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateMethod() {
        try {
            //1.
            Class<?> c1 = Class.forName("studentdemo.Student");
            Method method = c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);

            Student student = (Student) c1.newInstance();
            method.invoke(student,"我是一个参数...");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        /*reflectInstance();
        reflectPrivateConstructor();
        reflectPrivateField();*/
        reflectPrivateMethod();

        /*try {
            //1.
            Class<?> c1 = Class.forName("studentdemo.Student");

            //2.
            Class c2 = Student.class;

            //3.
            Student student = new Student();
            Class<?> c3 = student.getClass();
            System.out.println(c3.equals(c2));
            System.out.println(c2.equals(c1));
            System.out.println(c1.equals(c3));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }*/
    }
}
